package com.nengchuan.eic.nceconomy.util;

import com.nengchuan.eic.nceconomy.constant.CommonConstant;
import com.nengchuan.util.NullCheckUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: Httputils
 * @author: panwen
 * @create: 2020/05/20 15:54
 * @version：1.0.0
 */
public class HttpUtils {

    /**
     * 获取数据操作类型
     * @param pkId 主键
     * @return 操作常量
     */
    public static String getOperType(Long pkId){
        if(NullCheckUtils.isNull(pkId)){
            return CommonConstant.NO_IDEMPOTENCE_OPER_INSERT;
        }
        return CommonConstant.NO_IDEMPOTENCE_OPER_UPDATE;
    }

    /**
     * 获取请求对象
     * @return 请求
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes servletRequestAttributes = getServletRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }

    /**
     * 获取响应对象
     * @return 响应
     */
    public static HttpServletResponse getResponse(){
        ServletRequestAttributes servletRequestAttributes = getServletRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return response;
    }

    /**
     * 从ThreadLocal中获取全局RequestAttributes
     * @return 请求属性集合
     */
    private static ServletRequestAttributes getServletRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }
}
